package com.happy.film.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.happy.film.entity.Film;
import com.happy.film.entity.dto.FilmDto;
import com.happy.film.entity.vo.FilmVo;
import com.happy.film.entity.vo.PageVo;
import com.happy.film.mapper.FilmMapper;
import com.happy.film.service.IFilmService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.happy.film.utils.Query;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author wei.zhang
 * @since 2020-03-17
 */
@Service
public class FilmServiceImpl extends ServiceImpl<FilmMapper, Film> implements IFilmService {

    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;

    @Override
    public PageVo search(FilmDto filmDto) {
        IPage page = new Query().getPage(filmDto);
        LambdaQueryWrapper<Film> lambdaQueryWrapper = new LambdaQueryWrapper<Film>();
        if (FilmDto.search_type_byname.equals(filmDto.getSearchType())) {
            lambdaQueryWrapper.like(Film::getTitle, filmDto.getTitle());
            page = this.page(page, lambdaQueryWrapper);

        } else {
            page = this.baseMapper.selectFilmByTagIds(page,filmDto.getTagId());
        }
        PageVo pageVo = new PageVo(page);
        threadPoolExecutor.execute(() -> logSearch(filmDto));
        return pageVo;
    }

    public void logSearch(FilmDto filmDto) {

    }

    @Override
    public PageVo listFilms(FilmDto filmDto) {
        return null;
    }

    @Override
    public FilmVo info(String filmId) {
        Film film = this.getById(filmId);
        FilmVo filmVo = convert(film);
        return filmVo;
    }

    private FilmVo convert(Film film) {
        FilmVo filmVo = new FilmVo();
        BeanUtils.copyProperties(film, filmVo);
        return filmVo;
    }
}
